package com.fast.ThreadPoolFactory;

import cn.hutool.core.thread.NamedThreadFactory;

import java.util.concurrent.*;

/**
 * CompletableFuture有无返回值
 * runAsync    无
 * supplyAsync 有
 *
 * @author hjw
 * @since 2022年07月17日 16:42:00
 */
public class CF1_runAsync_supplyAsync {
   public static ThreadPoolExecutor threadPoolExecutor =  new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors() * 2,
            Runtime.getRuntime().availableProcessors() * 2,
            10,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<>(300),
            new NamedThreadFactory("core_thread_pool_",true),
                new ThreadPoolExecutor.AbortPolicy()
                        );

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始了-----------");
        //runAsync无返回值
        CompletableFuture.runAsync(() -> {
            System.out.println("runAsync线程"+Thread.currentThread().getName()+"--------把任务执行完了");
        }, threadPoolExecutor);

        //有返回值
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("supplyAsync线程"+Thread.currentThread().getName()+"--------把任务执行完了");
            return 100;
            }, threadPoolExecutor);
        System.out.println("future1 = " + future1.get());

        System.out.println("主线程结束了------------");
    }
}
